import {
  list as signInList,signIn
} from '../../../../api/signIn'
import {
  list as signInRecordList
} from '../../../../api/signInRecord'
Page({
  data: {
     signInGiftList:[],
     isSignIn:false,
     banner_image:'https://rzico-weex.oss-cn-hangzhou.aliyuncs.com/img/signIn1.png',
     checkRecord:[],
     signInRecord:[],
     cycle:'',
     signInId:"1",
     promotionForm1:[],
      newPromotion1: false,
  },
  onLoad() {
      this.getdata()
  },
  signInClick(){
    var _this = this
    signIn({signInId:this.data.signInId}).then(res=>{
        if(res.data.signInRecord.signInGift.type == 1){
          _this.data.isSignIn = false
          _this.setData({
            isSignIn:_this.data.isSignIn
          })
          _this.getdata()
          this.data.newPromotion1 = true
          this.setData({
            newPromotion1:this.data.newPromotion1
          })
          if (res.data.couponCodeVoList.length > 0) {
            res.data.couponCodeVoList.forEach(function (item) {
              item.endDate = _this.formatDateAndTime(item.endDate)
            })
            _this.data.promotionForm1 = res.data.couponCodeVoList
            this.setData({
              promotionForm1:this.data.promotionForm1
            })
          }
        }else{
          _this.data.isSignIn = false
          _this.setData({
            isSignIn:_this.data.isSignIn
          })
          _this.getdata()
          this.data.newPromotion1 = true
          this.setData({
            newPromotion1:this.data.newPromotion1
          })
        }
        }).catch(res=>{
          if(res.content == "今日已签到"){
            _this.data.isSignIn = true
            _this.setData({
              isSignIn:_this.data.isSignIn
            })

          }
        })
   },
  getList(resolve) {
    let _this = this
    const p1 = new Promise((resolve,reject)=>{
      signInList().then(res=>{
        if(res.data.data.length>0){
          _this.data.cycle = res.data.data[0].cycle
          _this.data.signInId = res.data.data[0].id
          _this.data.signInGiftList = res.data.data[0].signInGiftList
          _this.setData({
            cycle: _this.data.cycle,
            signInId:_this.data.signInId,
            signInGiftList:_this.data.signInGiftList,
          })
        }
      })
      resolve('P1请求成功')
    });
    const p2 = new Promise((resolve,reject)=>{
      signInRecordList().then(res=>{
        _this.data.isSignIn = res.data.isSignIn
        _this.data.signInRecord = res.data.signInRecord
        _this.setData({
          isSignIn: _this.data.isSignIn,
          signInRecord:_this.data.signInRecord,
        })
        resolve('P2请求成功')
      })
    });
    Promise.all([p1,p2]).then(data=>{
      resolve()
      console.log(data)  // data为一个数组  ['请求成功','上传成功']
    }).catch(err=>{
      console.log(err)
    })
  },
  getdata(){
    let  _this = this
    new Promise((resolve, reject) => {
      _this.getList(resolve)
    }).then((data) => {
        let i = 0
        for(i; i< _this.data.signInRecord.length;i++){
          _this.data.signInGiftList[i] =  _this.data.signInRecord[i].signInGift;
          _this.data.signInGiftList[i].isSignIn = true;
        }
        _this.setData({
          signInGiftList: _this.data.signInGiftList,
      })
    })
  },
  //时间格式化器
  formatDateAndTime: function (value) {
    let date = new Date(value);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    return y + '-' + MM + '-' + d + " " + h + ":" + m + ":" + s;
  },
  clickclose() {
    this.data.newPromotion1 = false
    this.setData({
      newPromotion1: this.data.newPromotion1
    })
  },
});
